﻿using IronPython.Hosting;
using jlynet.core.Library;
using jlynet.core.Library.Log;
using jlynet.core.notebook.util;
using Microsoft.Scripting.Hosting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace jlynet.core.notebook.myform
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
		
		//创建数据库的表
         private Boolean CreateTable()
         {
             try
             {
                SQLiteConnection myconn = new SQLiteConnection("DataSource=mysdf.db");
                SQLiteCommand com = new SQLiteCommand("create table PC (PCNO NVarChar(10) primary key,XH NVarChar(10) NOT NULL,WERKS NVarChar(4) NOT NULL,PC_DATE NVarChar(10))");
                 myconn.Open();
                 com.Connection = myconn;
                 com.ExecuteNonQuery();
                 myconn.Close();
                 MessageBox.Show("创建数据库成功！");
                 return true;
             }
             catch (Exception ex)
             {
                 MessageBox.Show("创建数据库失败" + ex.ToString().Substring(1, 60));
                 return false;
             }       
         }

        private void button1_Click(object sender, EventArgs e)
        {
            //RSACryption rsa = new RSACryption();
            //string xmlKeys = "";
            //string xmlPublicKey ="";
            //rsa.RSAKey(out xmlKeys, out xmlPublicKey);
            //LogHelper.info("xmlKeys: " + xmlKeys);
            //LogHelper.info("xmlPublicKey: " + xmlPublicKey);

            string a = DESEncrypt.Encrypt("pdHuHlOetdkJ/ceDzmaZJiQz0X4BHp+tVepf9wVZDUAoz/a6FFcU6WUSMRIjmi94J/NASDwPrmseqlx7MJMTXnFHV2vvlUI/q+JVwta45ixBEtrBPZI8Y0/DUZeYVM4xYQMEBa3qzRel19/7DdF3YSKOS5eA+su5Elbhw+BX7UE=", "SAUof5qBDemBCe0MheqQ7Px/Vyd7tAHVZsH/wUnc1kD+GOlIA8qYxhBEePSwaujgLrXbvyP7CZ3z+Tlmtf/DQqgRshXX3qudBfidUL0bF4tLc0N8ad74ucbsSm/4vyPus6x9W2+nnjiGpFmlMQv7y6Mh+eSlkiinxzkj+kptAgM=");
            string b = DESEncrypt.Decrypt(a, "SAUof5qBDemBCe0MheqQ7Px/Vyd7tAHVZsH/wUnc1kD+GOlIA8qYxhBEePSwaujgLrXbvyP7CZ3z+Tlmtf/DQqgRshXX3qudBfidUL0bF4tLc0N8ad74ucbsSm/4vyPus6x9W2+nnjiGpFmlMQv7y6Mh+eSlkiinxzkj+kptAgM=");
            LogHelper.info("加密: " + a);
            LogHelper.info("解密: " + b);




            //1、创建空的sqlite数据库。
            // 数据库名的后缀你可以直接指定，甚至没有后缀都可以
            // 方法一：创建一个空sqlite数据库，用IO的方式 
            //FileStream fs = File.Create("D://test.db");
            // 方法二：用SQLiteConnection 
            //SQLiteConnection.CreateFile("D://test.db");

            //2、创建加密的空sqlite数据库
            // 创建一个密码为password的空的sqlite数据库 
            // SQLiteConnection.CreateFile("D://test2.db");                
            //SQLiteConnection cnn = new SQLiteConnection("Data Source=D://test2.db");

            //SQLiteConnection cnn2 = new SQLiteConnection("Data Source=D://test2.db");
            //cnn2.Open();
            //cnn2.ChangePassword("password");

            //3、给未加密的数据库加密
            //SQLiteConnection cnn3 = new SQLiteConnection("Data Source=D://test.db");
            //cnn3.Open();
            //cnn3.ChangePassword(" password ");

            //4、打开加密sqlite数据库
            //方法一
            //SQLiteConnection cnn4 = new SQLiteConnection("Data Source=D://test2.db");
            //cnn4.SetPassword("password");
            //cnn4.Open();
            //方法二
            //SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
            //builder.DataSource = @"D:/test.db";
            //builder.Password = @"password";
            //SQLiteConnection cnn41 = new SQLiteConnection(builder.ConnectionString);
            //cnn41.Open();

            //注：
            //A、因为加密的函数是利用windows api，故加密后的数据库只能适用在windows平台，加密的方式是整体文件加密。
            //B、加密的算法是RC4，如果你想采用别的加密算法来加密，请参考ADO.NET 2.0 SQLite Data Provider 的源码来修改。
            //c、相关sqlite数据库操作类似ADO.NET 2.0。详见ADO.NET 2.0 SQLite Data Provider的帮助文档。
            //c、ADO.NET 2.0 SQLite Data Provider 版本为：1.0.53.0 ，SQLite版本: 3.6.0。
            //d、开发环境为vs2008。

            //string connectString = @"Data Source=D:\SQLite.db;Pooling=true;FailIfMissing=false";
            ///*D:\sqlite.db就是sqlite数据库所在的目录,
            //它的名字你可以随便改的*/
            //SQLiteConnection conn = new SQLiteConnection(connectString);
            ////新建一个连接
            //conn.Open();
            ////打开连接,如果sqlite.db存在就正常打开,
            ////如果不存在则创建一个SQLite.db文件
            //SQLiteCommand cmd = conn.CreateCommand();
            //cmd.CommandText = "select * from orders";
            ////数据库中要事先有个orders表
            //cmd.CommandType = CommandType.Text;
            //using (SQLiteDataReader reader = cmd.ExecuteReader())
            //{
            //    while (reader.Read())
            //        Console.WriteLine(reader[0].ToString());
            //}


            //创建数据库
            /* try
            {
                if ( File.Exists("mysdf.db"))
                {
                    MessageBox.Show("数据库已经存在");           
                }
                else
                {
                    SqlCeEngine eng = new SqlCeEngine("DataSource=mysdf.db");
                    eng.CreateDatabase();
                    eng.Dispose();
                    MessageBox.Show("创建数据库成功");　　　             
                }
                Boolean rtu;
                rtu = CreateTable();
                if (rtu = false)
                {
                    MessageBox.Show("数据表已经存在！");
                }
                else
                {
                    MessageBox.Show("创建数据表成功！");
                }        
            }
            catch(Exception ex)
            {
                MessageBox.Show("创建数据库失败"+ ex.ToString().Substring(1,60));            
            } */



        }

        private void button2_Click(object sender, EventArgs e)
        {
            ScriptRuntime py = Python.CreateRuntime();
            dynamic obj = py.UseFile("./scripts/test.py");
            Console.WriteLine(obj.welcome("xxxx"));


            var scriptName = "./scripts/test.py";
            var scriptRuntime = ScriptRuntime.CreateFromConfiguration();
            var pythonEngine = scriptRuntime.GetEngine("Python");
            //var source = pythonEngine.CreateScriptSourceFromFile(scriptName);  
            //var scope = pythonEngine.CreateScope();  

            dynamic test = scriptRuntime.UseFile(scriptName);
            Console.WriteLine(test.Test().ToString());

            //Console.ReadKey();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            MsgBoxHelper.Show("程序已崩溃，请重启!", "系统崩溃", MsgBoxButtons.OKCancel, MsgBoxIcon.SysBreak);
        }
    }
}
